/*
 * @Author: wuqingchun
 * @Date: 2024-09-23 13:51:03
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-10-03 14:45:55
 */

#include "chat_errno.h"
#include "redis/chat_redis_publisher.h"
#include "redis/chat_redis_subpub.h"

#include "chat_config.h"
#include "chat_server.h"

int AsPublisher()
{
    CChatRedisPublisher objPublisher;

    int32_t iErr = objPublisher.Init();
    if (iErr != CHAT_ERR_OK)
    {
        printf("Init error\n");
        return 0;
    }

    iErr = objPublisher.Connect("192.168.56.102", 6379);
    if (iErr != CHAT_ERR_OK)
    {
        printf("Connect error\n");
        return 0;
    }

    char szMsg1[1024] = {"Hello"};
    char szMsg2[1024] = {"Redis"};

    int i = 0;
    while (true)
    {
        sprintf(szMsg1, "Hello %d", i);
        sprintf(szMsg2, "Redis %d", i);
        objPublisher.Publish("test-channel1", szMsg1);
        objPublisher.Publish("test-channel2", szMsg2);
        std::this_thread::sleep_for(std::chrono::seconds(1));
        i++;
    }

    objPublisher.Exit();

    return 0;
}

int AsSubscriber()
{
    CChatRedisSubPub objSubscriber;

    int32_t iErr = objSubscriber.Init();
    if (iErr != CHAT_ERR_OK)
    {
        printf("Init error\n");
        return 0;
    }

    iErr = objSubscriber.Connect("192.168.56.102", 6379);
    if (iErr != CHAT_ERR_OK)
    {
        printf("Connect error\n");
        return 0;
    }

    objSubscriber.Subscribe("test-channel1");

    while (true)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    objSubscriber.Exit();
}

int main(int argc, char** argv)
{
    // if (argc > 1)
    // {
    //     AsSubscriber();
    // }
    // else
    // {
    //     AsPublisher();
    // }

    int32_t iErr = CChatConfig::GetInstance().Init();
    if (iErr != CHAT_ERR_OK)
    {
        printf("Failed to init config, %d\n", iErr);
        return iErr;
    }

    CChatServer objServer;
    objServer.Start();

    return 0;
}